/*
 * AIEngine a new generation network intrusion detection system.
 *
 * Copyright (C) 2013-2023  Luis Campo Giralte
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 *
 * Written by Luis Campo Giralte <luis.camp0.2009@gmail.com> 
 *
 */
#include "HTTPInfo.h"

namespace aiengine {

void HTTPInfo::reset() {

	direction_ = FlowDirection::NONE; 
	content_length_ = 0; 
	data_chunk_length_ = 0; 
	have_data_ = false; 
	is_banned_ = false;
	total_requests_ = 0;
	total_responses_ = 0;
	response_code_ = 0; 
#if defined(PYTHON_BINDING) || defined(RUBY_BINDING) || defined(JAVA_BINDING)
	needs_release_ = false; 
#endif
	write_uri_ = false;
	matched_domain_name.reset();
	resetStrings(); 
}

void HTTPInfo::resetStrings() { 

	uri.reset(); 
	host_name.reset(); 
	ua.reset(); 
	ct.reset();
	filename.reset();
}

std::ostream& operator<< (std::ostream &out, const HTTPInfo &info) {

	out << " Req(" << info.getTotalRequests()
		<< ")Res(" << info.getTotalResponses()
		<< ")Code(" << info.getResponseCode() << ")";

	if (info.isBanned()) 
		out << " Banned";

	if (info.host_name) 
		out << " Host:" << info.host_name->name();

	if (info.ct) 
		out << " ContentType:" << info.ct->name();

	if (info.filename) 
		out << " Filename:" << info.filename->name();

	if (info.ua) 
		out << " UserAgent:" << info.ua->name();

        return out;
}

nlohmann::json& operator<< (nlohmann::json& out, const HTTPInfo& info) {

	out["requests"] = info.getTotalRequests();
	out["responses"] = info.getTotalResponses();
	out["status"] = info.getResponseCode();

	if (info.isBanned()) 
		out["banned"] = true; 

	if (info.host_name) 
		out["host"] = info.host_name->name();

	if (info.ct) 
		out["ct"] = info.ct->name();

	if (info.filename) 
		out["filename"] = info.filename->name();

	if (info.ua) 
		out["ua"] = info.ua->name();

       	if (info.matched_domain_name) 
		out["matchs"] = info.matched_domain_name->name();

	return out;
}

} // namespace aiengine
